## Look for external dependencies.

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${nestk_SOURCE_DIR}/cmake)

# OpenGL
FIND_PACKAGE(OpenGL REQUIRED)

# X11
FIND_LIBRARY(X11_LIBRARY X11)
IF (NOT X11_LIBRARY)
  SET(X11_LIBRARY "")
ENDIF()

# GSL
IF (NESTK_USE_GSL)
  FIND_PACKAGE(GSL)
  IF (GSL_FOUND)
    ADD_DEFINITIONS(-DNESTK_USE_GSL)
    SET(GSL_LIBRARIES gsl gslcblas)
  ELSE()
    REMOVE_DEFINITIONS(-DNESTK_USE_GSL)
    SET(GSL_LIBRARIES "")
  ENDIF (GSL_FOUND)
ENDIF()

## OpenCV
FIND_PACKAGE(OpenCV REQUIRED)
IF (OpenCV_VERSION VERSION_GREATER 2.2)
  MESSAGE("OpenCV > 2.2 found, enable advanced features.")
  ADD_DEFINITIONS(-DHAVE_OPENCV_GREATER_THAN_2_2)
ENDIF()

IF(OpenCV_VERSION VERSION_GREATER 2.3.0)
  MESSAGE("OpenCV > 2.3.0 found, enable flann fix features.")
  ADD_DEFINITIONS(-DHAVE_OPENCV_GREATER_THAN_2_3_0)
ENDIF()

## Eigen
IF (NOT NESTK_USE_EMBEDDED_EIGEN)
  FIND_PACKAGE(Eigen3 REQUIRED)
  INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIR})  
ENDIF()
ADD_DEFINITIONS(-DNESTK_USE_EIGEN)
SET(NESTK_USE_EIGEN 1)

## Qt
SET(QT_USE_QTXML 1)
SET(QT_USE_QTSVG 1)
SET(QT_USE_QTOPENGL 1)
SET(QT_USE_QTNETWORK 1)
FIND_PACKAGE(Qt4 REQUIRED)
SET(NESTK_USE_QT 1)
ADD_DEFINITIONS(-DNESTK_USE_QT)
INCLUDE(${QT_USE_FILE})
IF (NOT QT4_FOUND)
  MESSAGE(FATAL_ERROR "Qt4 was not found. Check the QTDIR environment variable.")
ENDIF(NOT QT4_FOUND)

## GLEW
IF (NOT NESTK_USE_EMBEDDED_GLEW)
  FIND_PACKAGE(GLEW REQUIRED)
  INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR})
ENDIF()
SET(NESTK_USE_GLEW 1)
ADD_DEFINITIONS(-DNESTK_USE_GLEW)

## GLUT
FIND_PACKAGE(GLUT REQUIRED)
SET(NESTK_USE_GLUT 1)

## OpenNI
IF (NESTK_USE_OPENNI)
    ADD_DEFINITIONS(-DNESTK_USE_OPENNI)
    IF (NESTK_USE_EMBEDDED_OPENNI)
        ADD_DEFINITIONS(-DNESTK_USE_CUSTOM_OPENNI)
    ELSE()
        FIND_PACKAGE(OpenNI REQUIRED)
  	INCLUDE_DIRECTORIES(${OPENNI_INCLUDE_DIRS})
    ENDIF()
ELSE()
    REMOVE_DEFINITIONS(-DNESTK_USE_OPENNI)
ENDIF()

## Libfreenect
IF (NOT NESTK_USE_EMBEDDED_FREENECT AND NESTK_USE_FREENECT)
  FIND_PACKAGE(Freenect REQUIRED)
  INCLUDE_DIRECTORIES(${FREENECT_INCLUDE_DIR})
ENDIF()
IF (NESTK_USE_FREENECT)
    ADD_DEFINITIONS(-DNESTK_USE_FREENECT)
ELSE()
    REMOVE_DEFINITIONS(-DNESTK_USE_FREENECT)
ENDIF()

## PCL
IF(NESTK_USE_PCL)
  FIND_PACKAGE(PCL REQUIRED)
  SET(NESTK_USE_PCL 1)
  ADD_DEFINITIONS(-DNESTK_USE_PCL)

  if(WIN32)
    set(Boost_USE_STATIC_LIBS ON)
  endif(WIN32)

  SET(Boost_USE_MULTITHREADED ON)
  FIND_PACKAGE(Boost REQUIRED COMPONENTS date_time filesystem system thread)
  INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
  LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
  ADD_DEFINITIONS(${Boost_LIB_DIAGNOSTIC_DEFINITIONS})

  FIND_PACKAGE(Flann REQUIRED)
  INCLUDE_DIRECTORIES(${FLANN_INCLUDE_DIRS})

  FIND_PACKAGE(CMinpack REQUIRED)
  INCLUDE_DIRECTORIES(${CMINPACK_INCLUDE_DIRS})

  FIND_PACKAGE(Qhull REQUIRED)
  INCLUDE_DIRECTORIES(${QHULL_INCLUDE_DIRS})

  INCLUDE_DIRECTORIES(${PCL_INCLUDE_DIRS})
ELSE()
  SET(NESTK_USE_PCL 0)
  REMOVE_DEFINITIONS(-DNESTK_USE_PCL)
  SET(PCL_LIBRARIES "")
ENDIF()
